//-----------------------------------------------------------------------
// <author>Paul Long</author>
//-----------

/// <summary>
/// This is the main entry point into the program.  It will detect if a console is present, 
/// and if so attach to it so it can report help back or errors about the command line.
/// 
/// Program will parse the command line, using codeplex parsing module called CommandLine.
/// If the command line is just dumping information, then NMCaptureData is called with the
/// specified capture file.  Then NmCaptureData.DumpData is called.
/// 
/// If the interactive mode is called, then TopUsers is instantiated, and this runs the UI.
/// </summary>
/// <remarks>
/// TopUsers.cs - Main UI application.
/// 
/// NMCaptureData.cs - Does all the parsing of the capture data.  This is where all NMAPI are
/// called from.
/// 
/// NetworkElement.cs - Contains a class to encapulate an element which is an endpoint or
/// conversation between two endpoints depending on how TopUsers is called.
/// 
/// NetworkAddr.cs - A base class of Address for handling address managment for IPv4, IPv6 and 
/// Ethernet addresses.  This allows the us to generalize things like it's display and 
/// comparison for sorting.
/// 
/// MachineAddress.cs - Derived from the Address base class for handling machine addresses like 
/// ethernet and wireless.
/// 
/// IPv4Address.cs - Derived from Address base class.  Handles IPv4 addresses.
/// 
/// IPv6Address.cs - Derived from Address base class.  Handles IPv6 addresses.
/// 
/// Exception.cs - General Exception handling for parser errors.
/// 
/// Params.cs - class that uses CommandLine to parse and remember command line arguments.
/// 
/// BarChart.cs - Creates the Bar Chart
/// 
/// PieChart.cs - Creates the Pie Chart
/// 
/// NativeMethods.cs - Access to native code methods, currently AttachConsole.
/// </remarks>
/// 
namespace NMTopUsers
{
    using System;
    using System.Windows.Forms;
    using Microsoft.NetworkMonitor.Automation;
    using TopUserExceptions;

    /// <summary>
    /// Main program entry point class.
    /// </summary>
    internal sealed class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Command Line Args</param>
        [STAThread]
        public static void Main(string[] args)
        {
            bool haveConsole = false;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (NativeMethods.AttachConsole(-1))
            {
                haveConsole = true;
            }

            CommandLineArguments commandReader = new CommandLineArguments();

            if (commandReader.ParseCommandLineArguments(args) && !commandReader.IsRequestingHelp)
            {
                if (commandReader.IsNoArguments)
                {
                    Application.Run(new TopUsers(commandReader.CaptureFileName, commandReader.CombinedFilter, TopUsers.OperationMode.Endpoint));
                }
                else if (!string.IsNullOrEmpty(commandReader.DumpFile))
                {
                    try
                    {
                        NetworkMonitorData netmonData = new NetworkMonitorData(commandReader.CombinedFilter, commandReader.Mode);

                        if (NetworkMonitorData.ErrorsExist)
                        {
                            MessageBox.Show(
                                        NetworkMonitorData.GetErrors, 
                                        "Build Errors", 
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information,
                                        MessageBoxDefaultButton.Button1,
                                        MessageBoxOptions.DefaultDesktopOnly);
                            MessageBox.Show(
                                        "Please fix Parser Errors and restart", 
                                        "Build Errors",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information,
                                        MessageBoxDefaultButton.Button1,
                                        MessageBoxOptions.DefaultDesktopOnly);
                        }
                        else
                        {
                            if (netmonData.LoadCapture(commandReader.CaptureFileName, null, null, null))
                            {
                                netmonData.DumpData(commandReader.DumpFile);
                            }
                        }
                    }
                    catch (ParsingException exception)
                    {
                        MessageBox.Show(
                                        "Error loading " + exception.Message, 
                                        "Loading Error",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information,
                                        MessageBoxDefaultButton.Button1,
                                        MessageBoxOptions.DefaultDesktopOnly);
                    }
                }
                else
                {
                    Application.Run(new TopUsers(commandReader.CaptureFileName, commandReader.CombinedFilter, commandReader.Mode));
                }
            }
            else
            {
                if (commandReader.IsRequestingHelp)
                {
                    if (haveConsole)
                    {
                        Console.WriteLine(CommandLineArguments.GetUsage(AboutBox.AssemblyProduct));
                    }
                    else
                    {
                        MessageBox.Show(
                                CommandLineArguments.GetUsage(AboutBox.AssemblyProduct),
                                "Command Line Help",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information,
                                MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.DefaultDesktopOnly);
                    }
                }
                else
                {
                    if (haveConsole)
                    {
                        Console.WriteLine(commandReader.LastErrorMessage);
                    }
                    else
                    {
                        MessageBox.Show(
                                commandReader.LastErrorMessage,
                                "Command Line Help",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information,
                                MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.DefaultDesktopOnly);
                    }
                }
            }
        }
    }
}